﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wne.Context;
using wne.core.Interface;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using wne.models;
using wne.models.Filter;


namespace wne.core
{
    //А нет, я
    public class AccountFacadeSql : AccountFacade
    {//123
        public override bool IsValidUserPass(string pUserName, string pPassword)
        {
            using (var context = new tpksEntities())
            {
                return context.User.FirstOrDefault(x => x.Login.Equals(pUserName) && x.Password.Equals(pPassword)) != null;
            }
        }

        public override bool IsAdmin(string pUserName)
        {
            using (var context = new tpksEntities())
            {
                return context.User.FirstOrDefault(x => x.Login.Equals(pUserName) 
                                                     && x.Admin_Flag.Equals("1")) != null;
            }
        }

        public override bool ChangeUserPassword(string pUserName, string pOldPassword, string pNewPassword)
        {
            using (var context = new tpksEntities())
            {
                var user = context.User.FirstOrDefault(x => x.Login.Equals(pUserName));
                if (user == null) return false;
                if (!user.Password.Equals(pOldPassword)) return false;
                user.Password = pNewPassword;
                context.SaveChanges();
                return true;
            }
        }

        public override void ForgotPassword(string pUserName)
        {
            throw new NotImplementedException();
        }

        public override int CurrentUserId(string pUserName)
        {
            using (var context = new tpksEntities())
            {
                var currentuser = context.User.FirstOrDefault(x => x.Login.Equals(pUserName));
                return currentuser.User_ID;
            }
        }



       
    }
}
